package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupBizDto;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupCommonService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupManageService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupMemberService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 20:49:52<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_BUSINESS_ACTION_PUSH)
public class GroupBusinessAction {

	@Resource
	private GroupService groupService;
	@Resource
	private GroupCommonService groupCommonService;
	@Resource
	private GroupMemberService groupMemberService;
	@Resource
	private GroupManageService groupManageService;

	/**
	 * 
	 * Date 2019-01-26 14:40:12<br>
	 * Description 获取个人加入群的数量
	 * 
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getGroupCountByGroupMemberUserId(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("count", groupCommonService.countGroupListByGroupMemberUserId(userId)));
	}

	/**
	 * 
	 * Date 2019-01-26 14:40:32<br>
	 * Description 获取个人加入的群列表
	 * 
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getGroupListByGroupMemberUserId(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("list", groupCommonService.getGroupListByGroupMemberUserId(userId)));
	}

	/**
	 * 
	 * Date 2019-01-26 14:54:27<br>
	 * Description 转让群
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage changeGroupOwner(SocketSession socketSession, @JsonDefine("data") GroupBizDto query) {
		Long oldOwnerUserId = socketSession.getKey();
		Info info = groupManageService.changeGroupOwner(query.getGroupId(), oldOwnerUserId, query.getNewOwnerUserId());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}

	/**
	 * 
	 * Date 2019-01-27 12:54:59<br>
	 * Description 退出群
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage quit(
			SocketSession socketSession,
			@JsonDefine("data") GroupBizDto query) {
		Long userId = socketSession.getKey();
		Info info = groupMemberService.quit(query.getGroupId(), userId);
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}
	
	/**
	 * 
	 * Date 2019-01-26 15:04:37<br>
	 * Description 解散群
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0005")
	public ResultMessage disband(
			SocketSession socketSession,
			@JsonDefine("data") GroupBizDto query) {
		Long ownerUserId = socketSession.getKey();
		Info info = groupManageService.disbandGroup(query.getGroupId(), ownerUserId);
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}
}
